package com.kgc.skconsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesAdvance;
import com.kgc.collection.pojo.SalesAdvanceRefund;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import com.kgc.skconsumer.feign.SalesAdvanceFeign;
import com.kgc.skconsumer.feign.SalesAdvanceRefundFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SalesAdvanceRefundController")
public class SalesAdvanceRefundController {

    private Logger logger = LoggerFactory.getLogger(SalesAdvanceRefundController.class);

    @Autowired
    private CwWorkFlowFeign cwWorkFlowFeign;

    @Autowired
    private SalesAdvanceRefundFeign salesAdvanceRefundFeign;

    @Autowired
    private SalesAdvanceFeign salesAdvanceFeign;

    @RequestMapping("/selectSalesAdvanceRefundByPage")
    public ResponseMsg selectSalesAdvanceRefundByPage(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController selectSalesAdvanceRefundByPage params{}",map.toString());
        ResponseMsg responseMsg=salesAdvanceRefundFeign.selectSalesAdvanceRefundByPage(map);
        logger.debug("SalesAdvanceRefundController selectSalesAdvanceRefundByPage responseMsg:{} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertSalesAdvanceRefund")
    public ResponseMsg insertSalesAdvanceRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController insertSalesAdvanceRefund params{}",map.toString());
        ResponseMsg responseMsg = salesAdvanceRefundFeign.insertSalesAdvanceRefund(map);
        ObjectMapper mapper = new ObjectMapper();
        SalesAdvanceRefund salesAdvanceRefund = mapper.convertValue(map.get("salesAdvanceRefund"), SalesAdvanceRefund.class);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",salesAdvanceRefund.getDocCode());//单据编号
        variables.put("type",salesAdvanceRefund.getTypeId());//流程类型
        //创建一个流程实例
        cwWorkFlowFeign.startProcess(variables);
        logger.debug("SalesAdvanceRefundController insertSalesAdvanceRefund responseMsg:{} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesAdvanceRefundByCode")
    public  ResponseMsg selectSalesAdvanceRefundByCode(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController selectSalesAdvanceRefundByCode params{}",map.toString());
        ResponseMsg responseMsg=salesAdvanceRefundFeign.selectSalesAdvanceRefundByCode(map);
        logger.debug("SalesAdvanceRefundController selectSalesAdvanceRefundByCode responseMsg:{} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateSalesAdvanceRefund")
    public  ResponseMsg updateSalesAdvanceRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController updateSalesAdvanceRefund params{}",map.toString());
        ResponseMsg responseMsg=salesAdvanceRefundFeign.updateSalesAdvanceRefund(map);
        logger.debug("SalesAdvanceRefundController updateSalesAdvanceRefund responseMsg:{} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/deleteSalesAdvanceRefund")
    public  ResponseMsg deleteSalesAdvanceRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController deleteSalesAdvanceRefund params{}",map.toString());
        ResponseMsg responseMsg=salesAdvanceRefundFeign.deleteSalesAdvanceRefund(map);
        logger.debug("SalesAdvanceRefundController deleteSalesAdvanceRefund responseMsg:{} ",responseMsg);
        return responseMsg;

    }

    @RequestMapping("/checkSalesAdvanceRefund")
    public  ResponseMsg checkSalesAdvanceRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesAdvanceRefundController checkSalesAdvanceRefund params{}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("SalesAdvanceRefundController checkSalesAdvanceRefund responseMsg:{} ",responseMsg);
        return responseMsg;
    }
}
